import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    userInfo: null,
    token: null
  }),

  getters: {
    isAuthenticated: (state) => !!state.token,
    getUserInfo: (state) => state.userInfo
  },

  actions: {
    // 登录
    login(role) {
      // 模拟登录，直接设置用户信息
      this.userInfo = {
        UserName: role === 'expert' ? '专家用户' : role === 'bidder' ? '投标人' : '管理员',
        Role: role
      }
      this.token = 'demo-token'
    },

    // 登出
    logout() {
      this.userInfo = null
      this.token = null
    }
  }
}) 

/**
 * 检查是否有token
 */
export const hasToken = () => {
  return !!localStorage.getItem('token');
};

/**
 * 获取token
 */
export const getToken = () => {
  return localStorage.getItem('token');
};

/**
 * 设置token
 */
export const setToken = (token) => {
  localStorage.setItem('token', token);
};

/**
 * 移除token
 */
export const removeToken = () => {
  localStorage.removeItem('token');
};